<?php
namespace modules\admin\form;

use idea\saf\sl\Form;
use helper\ErrorNo;
use library\Validator;

/**
 * AdminsGroups class file
 * admins_groups表单管理类
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: AdminsGroups.php 1 2013-05-18 14:58:59Z huan.song $
 * @package modules.admin.form
 * @since 1.0
 */
class AdminsGroups extends Form
{
	/**
	 * @var array 寄存表的所有字段
	 */
	protected $_attributes = array(
		'group_name' => array(
			'label' => '管理组名',
			'type' => 'text',
			'rule' => array(
				Validator::NOT_EMPTY => array(
					true,
					'请填写管理组名.'
				)
			)
		)
	);

	/**
	 * 通过group_id获取group_name
	 * @param integer $value
	 * @return string
	 */
	public function getNameById($value)
	{
		$ret = $this->getService()->getByPk('group_name', $value);
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return '';
		}

		return $ret['group_name'];
	}

	/**
	 * 将所有模型的值组成Select表单元素
	 * @return array
	 */
	public function getGroupsOptions()
	{
		$ret = $this->getService()->findAll();
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return array();
		}

		$data = array();
		foreach ($ret['data'] as $row) {
			$data[] = array(
				'value' => $row['group_id'],
				'label' => $row['group_name'],
			);
		}

		return $data;
	}

	/**
	 * 通过管理组ID获取管理组权限
	 * @param integer $value
	 * @return string
	 */
	public function getAuthById($value)
	{
		$ret = $this->getService()->getByPk('group_auth', $value);
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return '';
		}

		return $ret['group_auth'];
	}
}
